package main_package;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.Collection;
import java.util.HashMap;

public class FileParser {

	String fileName;

	public FileParser(String fileName) {
		this.fileName = fileName;
	}

	public Collection<MTSystem> Parse() {
		String str;
		FileReader fr = null;
		BufferedReader br = null;
		HashMap<String, MTSystem> nameSystemMap = new HashMap<String, MTSystem>();
		Sentence reference = null;
		try {
			fr = new FileReader(fileName);
			br = new BufferedReader(fr);

			// as long as the file has more lines to read
			while ((str = br.readLine()) != null) {
				if (!str.equals("")) {
					// if it's the reference line
					if (str.startsWith("reference"))
						reference = new Sentence(str.substring(str.indexOf(':') + 1));
					// if it's a translation line
					else if (!str.startsWith("source")) {
						String systemName = str.substring(0, str.indexOf(':'));
						// Add the system if doesn't exist
						if (!nameSystemMap.containsKey(systemName))
							nameSystemMap.put(systemName, new MTSystem(systemName));

						Sentence translation = new Sentence(str.substring(str.indexOf(':') + 2));
						// Add the reference and translation to the system
						nameSystemMap.get(systemName).AddReferenceTranslationPair(reference, translation);
					}
				}
			}

			fr.close();
			br.close();
		}
		catch (Exception e) {
			e.printStackTrace();
		}

		return nameSystemMap.values();
	}
}
